package com.sskj.mine.ui.activity;


import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.ImageUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.IdentityVerifyActivityPresenter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


/***
 * 身份认证
 */
@Route(path = RConfig.MINE_IDENTITY_VERIFY)
public class IdentityVerifyActivity extends BaseActivity<IdentityVerifyActivityPresenter> {
    @BindView(R2.id.etName)
    EditText etName;
    @BindView(R2.id.etIDCard)
    EditText etIDCard;
    @BindView(R2.id.ivFront)
    ImageView ivFront;
    @BindView(R2.id.ivCardBack)
    ImageView ivCardBack;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private boolean isFront;
    private String filePathFront;
    private String filePathBack;
    private AndroidNextInputs inputs;
    @Inject
    UserViewModel userViewModel;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_identity_verify;
    }

    @Override
    public IdentityVerifyActivityPresenter getPresenter() {
        return new IdentityVerifyActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_verifyHomeActivity1));
        DaggerUserDataComponent.create().inject(this);
        inputs = new ToastNextInputs();
        WidgetAccess access = new WidgetAccess(this);
        inputs
                .add(access.findEditText(R.id.etName), SchemeUtil.notEmpty(etName))
                .add(access.findEditText(R.id.etIDCard), SchemeUtil.notEmpty(etIDCard), StaticScheme.ChineseIDCard());

        ClickUtil.click(ivFront, () -> {
            isFront = true;
            selectPic();
        });
        ClickUtil.click(ivCardBack, () -> {
            isFront = false;
            selectPic();
        });

        ClickUtil.click(btSubmit, () -> {
            if (checkParam()) {
                mPresenter.identity(etName.getText().toString(),
                        etIDCard.getText().toString(), filePathFront, filePathBack);
            }
        });
    }

    private boolean checkParam() {
        if (!inputs.test()) {
            return false;
        }
        if (filePathFront == null) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_identityVerifyActivity2));
            return false;
        }
        if (filePathBack == null) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_identityVerifyActivity3));
            return false;
        }
        return true;
    }

    public void identitySuccess() {
        userViewModel.update();
        finish();
    }

    private void selectPic() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .selectionMode(PictureConfig.SINGLE)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    LocalMedia media = selectList.get(0);
                    String filePath;
                    if (media.isCompressed()) {
                        filePath = media.getCompressPath();
                    } else {
                        filePath = media.getPath();
                    }
                    if (isFront) {
                        filePathFront = filePath;
                        ImageUtil.setImage(filePath, ivFront);
                    } else {
                        filePathBack = filePath;
                        ImageUtil.setImage(filePath, ivCardBack);
                    }
                    break;
            }

        }
    }
}
